<?php

class ectools_mdl_currency extends dbeav_model {

    var $idColumn = 'cur_code';

    var $textColumn = 'cur_name';

    function __construct(&$app) {
        parent::__construct($app);
        $this->oMath = kernel::single('ectools_math');
    }
 // End Function
    public function getSysCur($pass_exist = false, $pass_except = '', $is_sign = true) {
        $return = array();
        $aFilter = array();
        if ($pass_exist) {
            $aFilter = $this->curAll(); // 获取已入库的所有货币
            
            $aFilter = $this->array_change_key($aFilter, 'cur_code');
            
            if (! empty($pass_except) && array_search($pass_except, $aFilter) !== false) {
                $key = array_search($pass_except, $aFilter);
                unset($aFilter[$key]);
            }
        }
        
        // 使用文件方式 读取一定格式文件
        foreach (file($this->app->app_dir . '/currency.txt') as $row) {
            list ($code, $sign, $cnname, $enname) = explode("\t", trim($row));
            if (in_array($code, $aFilter))
                continue;
            if ($is_sign)
                $return[$code] = $sign . ' ' . $cnname;
            else
                $return[$code] = $cnname;
        }
        
        return $return;
    }

    function array_change_key($aArray = array(), $aKey = '') {
        $tmpArray = array();
        if (is_array($aArray)) {
            foreach ($aArray as $subKey => $subVal) {
                $tmpArray[$subKey] = @$subVal[$aKey];
            }
        }
        return $tmpArray;
    }

    public function curAll() {
        $arr_curs = $this->getList('*', '', 0, - 1);
        foreach ($arr_curs as $key => $arr) {
            $arr_curs[$key]['cur_name'] = app::get('ectools')->_($arr_curs[$key]['cur_name']);
        }
        return $arr_curs;
    }

    public function getcur($curCode) {
        if (! $this->_in_cur || (isset($this->_in_cur['cur_code']) && $this->_in_cur['cur_code'] != $curCode)) {
            $filter = array(
                'cur_code' => $curCode
            );
            
            $aCur = $this->dump($filter, '*');
            if ($aCur['cur_code']) {
                return $this->_in_cur = $aCur;
            } else {
                if (! $aCur) {
                    $this->_default_cur = $this->db->selectrow('select * FROM ' . DB_PREFIX . 'ectools_currency WHERE cur_default="true"');
                    if (! isset($this->_default_cur) || ! $this->_default_cur) {
                        // 使用文件方式 读取一定格式文件
                        foreach (file($this->app->app_dir . '/currency.txt') as $row) {
                            list ($code, $sign, $cnname, $enname) = explode("\t", trim($row));
                            if (! in_array($code, $filter))
                                continue;
                            $this->_default_cur = array(
                                'cur_code' => $code,
                                'cur_name' => app::get('ectools')->_($cnname),
                                'cur_sign' => $sign,
                                'cur_rate' => 1
                            );
                            return $this->_in_cur = $this->_default_cur;
                        }
                    }
                }
                $this->_in_cur = $aCur;
                $arrDef = $this->getDefault();
                $this->_in_cur = $arrDef;
                return $this->_in_cur;
            }
        } else {
            return $this->_in_cur;
        }
    }

    public function getDefault() {
        if (! $this->_default_cur) {
            $defCur = $this->app->getConf('site.currency.defalt_currency');
            $filter = array(
                'cur_code' => $defCur
            );
            if (! ($this->_default_cur = $this->dump($filter, '*'))) {
                $this->_default_cur = $this->db->selectrow('SELECT * FROM `' . DB_PREFIX . 'ectools_currency` WHERE `cur_default`=\'true\'');
                if (! isset($this->_default_cur) || ! $this->_default_cur) {
                    // 使用文件方式 读取一定格式文件
                    foreach (file($this->app->app_dir . '/currency.txt') as $row) {
                        list ($code, $sign, $cnname, $enname) = explode("\t", trim($row));
                        if (! in_array($code, $filter))
                            continue;
                        $this->_default_cur = array(
                            'cur_code' => $code,
                            'cur_name' => app::get('ectools')->_($cnname),
                            'cur_sign' => $sign,
                            'cur_rate' => 1
                        );
                        return $this->_default_cur;
                    }
                }
            }
        }
        
        return $this->_default_cur;
    }

    public function set_currency_default() {
        $arrCurs = $this->curAll();
        if ($arrCurs)
            $this->update(array(
                'cur_default' => 'false'
            ), array());
    }

    // 一下方法需要调整
    public function getFormat($cur = null) {
        $ret = array();
        if (! $cur) {
            $cursign = $this->getDefault();
        } else {
            $cursign = $this->getcur($cur);
        }
        
        $ret['sign'] = $cursign['cur_sign'];
        return $ret;
    }

    public function changer($money, $currency = '', $is_sign = false, $is_rate = false) {
        // 异常处理
        if ($money === null) {
            $money = intval($money);
        }
        
        // 获取货币金额
        $cur_money = $this->get_cur_money($money, $currency);
        
        if ($is_rate) {
            $cur_money = $money;
        }
        
        // 格式化货币
        if ($is_sign) {
            return $this->formatNumber($cur_money, false, false);
        } else {
            return $this->_in_cur['cur_sign'] . $this->formatNumber($cur_money, false);
        }
    }

    public function changer_odr($money, $currency = '', $is_sign = false, $is_rate = false, $decimals = '2', $operation_carryset = '0') {
        // 异常处理
        if ($money === null) {
            $money = intval($money);
        }
        
        // 获取货币金额
        $cur_money = $this->get_cur_money($money, $currency);
        
        if ($is_rate) {
            $cur_money = $money;
        }
        
        $cur_money = $this->oMath->formatNumber($cur_money, $decimals, $operation_carryset);
        
        // 格式化货币
        if ($is_sign) {
            return $cur_money;
        } else {
            return $this->_in_cur['cur_sign'] . $cur_money;
        }
    }

    public function get_cur_money($money, $currency = '') {
        // 异常处理
        if ($money === null) {
            $money = intval($money);
        }
        
        if (empty($currency)) {
            $arrDef = $this->getDefault();
            $currency = ($this->system->request['cur']) ? $this->system->request['cur'] : $arrDef['cur_code'];
        }
        if ($currency) {
            $this->_in_cur = $this->getcur($currency);
        }
        
        return $this->oMath->number_multiple(array(
            $money,
            ($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1)
        ));
    }

    public function get_cur_name($currency = '') {
        if ($currency == '')
            return '';
        
        $this->_in_cur = $this->getcur($currency, true);
        
        return $this->_in_cur['cur_name'];
    }

    public function amount($money, $currency = '', $basicFormat = false, $chgval = true) {
        // 异常处理
        if ($money === null) {
            $money = intval($money);
        }
        
        if (empty($currency)) {
            $arrDef = $this->getDefault();
            $currency = ($this->system->request['cur']) ? $this->system->request['cur'] : $arrDef['cur_code'];
        }
        if ($currency) {
            $this->_in_cur = $this->getcur($currency);
        }
        
        if ($chgval) {
            $money = $this->oMath->number_multiple(array(
                $money,
                ($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1)
            ));
        }
        
        $money = $this->oMath->getOperationNumber($money);
        $money = $this->formatNumber($money);
        
        if ($basicFormat) {
            return $money;
        }
        
        $precision = $this->app->getConf('site.decimal_digit.display');
        
        return $this->_in_cur['cur_sign'] . $money;
    }

    public function amount_nocur($money, $currency = '', $basicFormat = false, $chgval = true) {
        // 异常处理
        if ($money === null) {
            $money = intval($money);
        }
        
        if (empty($currency)) {
            $arrDef = $this->getDefault();
            $currency = ($_REQUEST['cur']) ? $_REQUEST['cur'] : $arrDef['cur_code'];
        }
        if ($currency) {
            $this->_in_cur = $this->getcur($currency);
        }
        if ($chgval) {
            $money = $money * ($this->_in_cur['cur_rate'] ? $this->_in_cur['cur_rate'] : 1);
        }
        
        $money = $this->oMath->getOperationNumber($money);
        // $money = $this->formatNumber($money);
        
        if ($basicFormat) {
            return $money;
        }
        
        $precision = $this->app->getConf('site.decimal_digit.display');
        
        return $money;
    }

    public function formatNumber($number, $is_count = true, $is_str = true) {
        // 异常处理
        if ($number === null) {
            $money = intval($money);
        }
        
        // 取到格式化后的数值
        $number = $this->oMath->getOperationNumber($number, $is_count);
        
        if (! $is_count)
            $this->_money_format['decimals'] = $this->oMath->goodsShowDecimals;
        else
            $this->_money_format['decimals'] = $this->oMath->operationDecimals;
        
        if ($is_str) {
            return number_format(trim($number), $this->_money_format['decimals'], $this->_money_format['dec_point'], $this->_money_format['thousands_sep']);
        } else
            return number_format(trim($number), $this->_money_format['decimals'], '.', '');
    }
}
?>
